package com.barunsw.bwf.ems.neoarena.ne;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import NAS.NASIf;
import NAS.NASIfHelper;

import com.barunsw.bwf.ems.dto.NeIorVo;
import com.barunsw.bwf.remote.corba.IorTextOrbSessionFactory;
import com.barunsw.bwf.remote.corba.OrbSession;

@Component
public class NASIfManager {
	private static final Logger LOGGER = LoggerFactory.getLogger(NASIfManager.class);

	@Autowired
	NeIorDao neIorDao;
	
	@Autowired
	IorTextOrbSessionFactory orbSessionFactory;
	
	private Map<Long, NASIf> nasIfMap = new HashMap<Long, NASIf>();
	
	public NASIf getNASIf(long nodeId) {
		LOGGER.debug("nodeId:" + nodeId);
		
		NASIf nasIf = nasIfMap.get(nodeId);
		
		if ( nasIf == null ) {
			NeIorVo neIorVo = neIorDao.select(nodeId);
			if ( neIorVo != null ) {
				orbSessionFactory.setIorText(neIorVo.getIorText());
				OrbSession orbSession = orbSessionFactory.getSession();

				nasIf = NASIfHelper.narrow(orbSession.getObject());
				
				nasIfMap.put(nodeId, nasIf);
			}	
		}
		
		return nasIf;
	}
}
